new Vue({
    el: ".products",
    data: {
        carList: [],
        totalMoney: 0
    },
    methods: {
        findCars: function () {
            axios({
                url: "/car/findWaitPay"
            }).then(res => {
                if (res.data.flag) {
                    this.carList = res.data.data;
                    this.countTotalMoney();
                }
            });
        },
        countTotalMoney: function () {
            this.totalMoney = 0;
            for (let i = 0; i < this.carList.length; i++) {
                let car = this.carList[i];
                this.totalMoney += (car.goodsPrice * car.goodNum);
            }
        },
        deleteCar: function (carId) {
            layer.confirm('确定删除该商品？', {
                btn: ['确定', '我再想想'], //按钮
                yes: () => {
                    axios({
                        method: "delete",
                        url: `/car/${carId}`
                    }).then(res => {
                        layer.msg(res.data.message);
                        if (res.data.flag) {
                            this.findCars();
                        }
                    });
                }
            });
        },
        doPay: function (carId,totalMoney) {
            layer.confirm(`共${totalMoney}元`, {
                btn: ['确定支付', '我再想想'], //按钮
                yes: () => {
                    axios({
                        method: "put",
                        url: `/car/doPay/${carId}`
                    }).then(res => {
                        if (res.data.flag) {
                            this.findCars();
                            layer.msg("支付成功");
                        }
                    });
                }
            });
        },
        doPayAll: function () {
            layer.confirm(`共${this.totalMoney}元`, {
                btn: ['确定支付', '我再想想'], //按钮
                yes: () => {
                    axios({
                        method: "put",
                        url: "/car/doPayAll"
                    }).then(res => {
                        if (res.data.flag) {
                            this.findCars();
                            layer.msg("支付成功");
                        }
                    });
                }
            });
        },
        toDetail: function (goodsId) {
            sessionStorage.setItem("goodsId",goodsId);
            location.href = "goodsDetail.html"
        }
    },
    created: function () {
        this.findCars();
    }
})